<?php
require_once("phpmailer/class.phpmailer.php");
define('ROOT_PATH', dirname(__FILE__));
?>
<h1>S3 Birthday Annoucement</h1>
<?php
class BA{
	public  function sendAnnouncement(){
		// Initial wishs & cakes
		$wishs = array();
		$wishs[]="May every pleasure in life come your way, today and every day.<br/>Have a fantastic birthday!";
		$wishs[]="I hope all your birthday dreams and wishes come true.<br/>Have a fantastic birthday!";
		$wishs[]="Wishing you all the great things in life! <br/>Hope this day will bring you an extra share of all that makes you happiest.";
		$wishs[]="May this year bring with it all the success and fulfillment your heart desires.<br/>Happy birthday!";
		$wishs[]="Hoping this card bring you our sincere greetings.<br/>You will be blessed through the coming year in fullest measure!";
		$wishs[]="On such a day like your birthday,<br/>May you be in arm with a truly happy day bringing fulfillment of your favorite hope your brightest promise!";
		$wishs[]="Send you the beautiful blessing that is as sweet as a flower to be your companion till the end of the world.<br/>Happy birthday to you!";
		$wishs[]="We want to wish you love and happiness on your birthday.<br/>Have a fantastic birthday!";
		$wishs[]="Wishing you health and happiness in the year to come.<br/>Have a fantastic birthday!";
		$wishs[]="Wishing your path is very spacious, you will have full energy and confident to get desirable things!";
		$wishs[]="Congratulation your birthday, we wish you young and nice forever and happiness!<br/>Have a fantastic birthday!";
		$wishs[]="On occasion of your  birthday, wishing you happy and beautiful, young.<br/>The best wishes to you with the most lucky, the best and most happiness will come to you in new age!";
		$wishs[]="Hoping this card bring your our sincere greetings. You will be blessed through the coming year in fullest measure. <br/>Happy birthday!";
		$wishs[]="Sending you this present with our hearts and with that you'll be happy in fullest measure. <br/>May the happiest things always happen to you!";
		$wishs[]="We hope that the coming year bring you peace and prosperity. <br/>Happy birthday to you!";
		$wishs[]="Wishing you health and happiness on your birthday and for many years to come.<br/>Have a fantastic birthday!";
		$wishs[]="Congratulations and sincere good wishes on your birthday.<br/>Have a fantastic birthday!";
		$wishs[]="Hope that the gift will add in some small measure to all the joy and happiness we wish you on your birthday!";
		$wishs[]="Your birthday's coming!!!! <br/>We hope you always have smile  in your mouth, and when you more years you'll stately, and smile always next to you";
		$wishs[]="Happy birthday to you! <bg/>We wish you always have a nice day, always smile in your life, everything in your life is the best, and today we hope you will have a great dream.";
		$wishs[]="Send you our beautiful wishes that are as sweet as a flower to be your companion till the end of the world. <br/>Happy birthday to you!";
		$wish = array_rand($wishs,1);

		$birthdaycake = array();
		/*$birthdaycake[]="http://intranet/BA/images/sn01.gif";
		$birthdaycake[]="http://intranet/BA/images/sn02.gif";
		$birthdaycake[]="http://intranet/BA/images/sn03.gif";
		$birthdaycake[]="http://intranet/BA/images/sn04.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn05.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn06.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn07.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn08.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn09.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn10.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn11.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn12.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn13.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn14.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn15.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn16.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn17.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn18.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn19.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn20.jpeg";
		$birthdaycake[]="http://intranet/BA/images/sn21.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn22.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn23.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn24.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn25.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn26.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn27.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn28.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn29.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn30.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn31.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn32.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn33.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn34.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn35.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn36.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn37.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn38.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn39.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn40.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn41.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn42.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn43.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn44.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn45.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn46.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn47.jpg";
		$birthdaycake[]="http://intranet/BA/images/sn48.jpg";*/

		$birthdaycake[]="images/sn01.gif";
		$birthdaycake[]="images/sn02.gif";
		$birthdaycake[]="images/sn03.gif";
		$birthdaycake[]="images/sn21.jpg";
		$birthdaycake[]="images/sn22.jpg";
		$birthdaycake[]="images/sn23.jpg";
		$birthdaycake[]="images/sn24.jpg";
		$birthdaycake[]="images/sn25.jpg";
		$birthdaycake[]="images/sn26.jpg";
		$birthdaycake[]="images/sn27.jpg";
		$birthdaycake[]="images/sn28.jpg";
		$birthdaycake[]="images/sn30.jpg";
		$birthdaycake[]="images/sn31.jpg";
		$birthdaycake[]="images/sn33.jpg";
		$birthdaycake[]="images/sn34.jpg";
		$birthdaycake[]="images/sn35.jpg";
		$birthdaycake[]="images/sn36.jpg";
		$birthdaycake[]="images/sn37.jpg";
		$birthdaycake[]="images/sn38.jpg";
		$birthdaycake[]="images/sn39.jpg";
		$birthdaycake[]="images/sn41.jpg";
		$birthdaycake[]="images/sn42.jpg";
		$birthdaycake[]="images/sn43.jpg";
		$birthdaycake[]="images/sn46.jpg";
		$birthdaycake[]="images/sn47.jpg";
		$birthdaycake[]="images/sn48.jpg";

		$birthdayindex = array_rand($birthdaycake,1);

		//Get birthday infomation
		$cnx = mysql_connect('localhost','root','S3PhatDat@X');
		mysql_select_db('hrm');

		$employees = @mysql_query("SELECT * FROM hs_hr_employee where MONTH(`emp_birthday`)=MONTH(NOW()) AND DAYOFMONTH(`emp_birthday`)=DAYOFMONTH(NOW()) AND emp_status != 'EST000'");
		if (!$employees) {
		  die('<p>Error retrieving employees from database!<br />'.
		      'Error: ' . mysql_error() . '</p>');
		}
		$empNames = array();
		$empEmails = array();
		while ($employee = mysql_fetch_array($employees)) {
			$empNames[] = $employee["emp_lastname"]." ".$employee["emp_middle_name"]." ".$employee['emp_firstname'];
			$empEmails[] = $employee["emp_work_email"];
		}

		if(count($empNames)>0){

			$str = implode(", ",$empNames);
			$mail                = new PHPMailer();
			$mail->IsSMTP();
                        $mail->Timeout = 90;
                        $mail->SMTPDebug = true;
			$mail->Host = "mail.s3corp.com.vn";
			$mail->SMTPAuth = true;
                        $mail->Username ='send-brithday@s3corp.com.vn';
                        $mail->Password ='12345678S3@X';
			$mail->IsHTML(true);
			$mail->Subject= "Happy birthday $str";
			$mail->From="hr@s3corp.com.vn";
			$mail->FromName="Human Resources";
			for($i=0;$i<count($empEmails);$i++){
				$address=$empEmails[$i];
				$mail->AddAddress($address,$empNames[$i]);
				$mail->AddReplyTo($address,$empNames[$i]);
			}
			$mail->AddEmbeddedImage(ROOT_PATH.'/'.$birthdaycake[$birthdayindex],'sncake','sncake');
			$mail->AddEmbeddedImage(ROOT_PATH.'/images/logo.PNG','logo','logo.PNG');
			$mail->AddCC("s3everyone@s3corp.com.vn","Everyone");
			$mail->Body ="<span style='font-family:.VnAristote;font-size:20px;color:#606062;'>Dear $str,</span><br/>"
			             ."<span style='font-family:.VnAristote;font-size:20px;color:#606062;'>".$wishs[$wish]."</span>"
			             ."<br/><img style='border:0px;' src='cid:sncake' width ='400' height='300' />"
			             ."<br/><br/><span style='font-family:.VnAristote;font-size:20px;color:#606062;'>Best regard,</span>"
			             ."<br/><img style='border:0px;' src='cid:logo' width ='187' height='89' />";

                        for($i=0;$i<10;$i++){
		  	if($mail->Send()) {
		  		echo "Message sent successfull";
                                break;
		  	} else {
		  		echo $mail->ErrorInfo;
		  	}
                        //error_log(date('r').' : '.strip_tags($mail->Debugoutput)."\r\n", 3, '/var/log/birthday.txt');
                         error_log(date('r').' : '.$mail->ErrorInfo."\r\n", 3, '/var/log/birthday.txt');

                        }
		}
	}

}
$annoucement = new BA();
$annoucement->sendAnnouncement();
?>

